package com.axc.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.axc.system.domain.DictinoaryDO;
import com.axc.system.domain.LayUiPager;
import com.axc.system.service.DictinoaryService;
import com.axc.common.utils.R;

/**
 * 
 * 
 * @author admin
 * @email yfq900108@163.COM
 * @date 2018-08-01 10:29:25
 */
 
@Controller
@RequestMapping("/system/dictinoary")
public class DictinoaryController {
	@Autowired
	private DictinoaryService dictinoaryService;
	
	@GetMapping()
	@RequiresPermissions("system:dictinoary:dictinoary")
	String Dictinoary(){
	    return "system/dictinoary/dictinoary";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:dictinoary:dictinoary")
	public LayUiPager<DictinoaryDO> list(String dictinoaryType){
		//查询列表数据
		Map<String, Object> query = new HashMap<>(16);
		query.put("dictinoaryType", dictinoaryType);
		List<DictinoaryDO> dictinoaryList = dictinoaryService.list(query);
	
		LayUiPager<DictinoaryDO> pageUtils = new LayUiPager<DictinoaryDO>();
		
		DictinoaryDO[] strings = new DictinoaryDO[dictinoaryList.size()];
		dictinoaryList.toArray(strings);
		pageUtils.setdata(dictinoaryList.toArray(strings));
		pageUtils.setcode(Long.parseLong("0"));
		pageUtils.setmsg("");
		pageUtils.setcount(dictinoaryList.size());
		
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:dictinoary:dictinoary")
	String add(){
	    return "system/dictinoary/add";
	}
	

	@ResponseBody
	@GetMapping("/get_dictinoray_list_by_code")
	public List<DictinoaryDO> get_dictinoray_list_by_code(String dictinoaryType){
		
		Map<String, Object> query = new HashMap<>(16);
		query.put("dictinoaryType", dictinoaryType);
		List<DictinoaryDO> sysDeptList = dictinoaryService.list(query);
		return sysDeptList;
	}
	
	@ResponseBody
	@GetMapping("/get_model_by_id")
	@RequiresPermissions("system:dictinoary:dictinoary")
	public DictinoaryDO get_model_by_id(Long id){
		DictinoaryDO dictinoary = dictinoaryService.get(id);
	    return dictinoary;
	}
	
	@ResponseBody
	@GetMapping("/get_model_by_query")
	@RequiresPermissions("system:dictinoary:dictinoary")
	public DictinoaryDO get_model_by_query(DictinoaryDO model){
		
		DictinoaryDO dictinoary = dictinoaryService.GetByModel(model);
	    return dictinoary;
	}
	
	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:dictinoary:dictinoary")
	String edit(@PathVariable("id") Long id,Model model){
		DictinoaryDO dictinoary = dictinoaryService.get(id);
		model.addAttribute("dictinoary", dictinoary);
	    return "system/dictinoary/edit";
	}
	
	@GetMapping("/edit")
	@RequiresPermissions("system:dictinoary:dictinoary")
	String edit(){
	    return "system/dictinoary/edit";
	}
	
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:dictinoary:dictinoary")
	public R save( DictinoaryDO model){
		
		DictinoaryDO model1= get_model_by_query(model);
		if(model1==null)
		{
			if(dictinoaryService.save(model)>0){
				return R.ok();
			}
			else {

				return R.error(2,"更新失败");
			}
		}
		else
		{
			return R.error(2,"本字典类型下，<br/>字典编码为："+model.getItemvalue()+";<br/>字典名称为："+model.getItemname()+"<br/>的记录已存在！");
		}
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:dictinoary:dictinoary")
	public R update( DictinoaryDO model){

		DictinoaryDO model1= get_model_by_query(model);
		if(model1==null||model1.getId()==model.getId())
		{
			int count=dictinoaryService.update(model);
			if(count==1)
			{
				return R.ok();
			}
			else {

				return R.error(2,"更新失败");
			}
		}
		else
		{
			return R.error(2,"本字典类型下，<br/>字典编码为："+model.getItemvalue()+";<br/>字典名称为："+model.getItemname()+"<br/>的记录已存在！");
		}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:dictinoary:dictinoary")
	public R remove( Long id){
		if(dictinoaryService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:dictinoary:dictinoary")
	public R remove(@RequestParam("ids[]") Long[] ids){
		dictinoaryService.batchRemove(ids);
		return R.ok();
	}
	
}
